package com.example.blueconn.activity;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;

import com.example.blueconn.Constant;
import com.example.blueconn.R;
import com.example.blueconn.databinding.ActivityCommonBinding;
import com.example.blueconn.fragment.BLSendParamFragment;
import com.example.blueconn.fragment.ShowDetailFragment;
import com.example.blueconn.fragment.bl.BLSelectFragment;
import com.example.blueconn.fragment.bl.BLViewParamFragment;

public class CommonActivity extends BaseActivity {

    private ActivityCommonBinding commonBinding;

    private String title = "";
    private int deviceNub = -1;
    private int fragmentTag = 0;

    @Override
    protected void initView() {
        commonBinding = DataBindingUtil.setContentView(this, R.layout.activity_common);
        commonBinding.topbar.setTitle(title);
        commonBinding.topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initFragment();
    }

    private void initFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (Constant.SHOW_CHART_DETAIL == fragmentTag) {
            transaction.add(R.id.container,
                    ShowDetailFragment.newInstance(getIntent().getExtras()),
                    ShowDetailFragment.TAG);
        }
        if (Constant.BL_SELECT_PAGER == fragmentTag) {
            transaction.replace(R.id.container, BLSelectFragment.newInstance());
        }
        if (Constant.BL_SEND_PARAM == fragmentTag) {
            transaction.replace(R.id.container, BLSendParamFragment.newInstance());
        }
        if (Constant.BL_VIEW_PARAM == fragmentTag) {
            transaction.replace(R.id.container, BLViewParamFragment.newInstance());
        }
        transaction.commitAllowingStateLoss();
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            title = bundle.getString(Constant.APP_BAR_TITLE);
            deviceNub = bundle.getInt(Constant.DEVICE_NUM);
            fragmentTag = bundle.getInt(Constant.FRAGMENT_TAG);
        }
    }
}